package org.shangrila.bme.mb;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.event.SelectEvent;
import org.shangrila.app.sys.cfg.AppInitInfo;
import org.shangrila.bme.eo.BmeContProd;
import org.shangrila.bme.eo.BmeContPurch;
import org.shangrila.bme.eo.BmeContract;
import org.shangrila.comm.dao.DbAccess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
@SessionScoped
public class ContractQueryMB implements Serializable {
	private static final long serialVersionUID = -5953392023664217112L;
	private final Logger log = LoggerFactory.getLogger(this.getClass());

    private BmeContract io = new BmeContract();//合同查询信息
	private List<BmeContract> voc = new ArrayList<BmeContract>(); //合同
	private BmeContract selectedContract = new BmeContract(); 
	private List<BmeContProd> vod = new ArrayList<BmeContProd>(); //产品
	private List<BmeContPurch> vop = new ArrayList<BmeContPurch>(); //产品

	//获取相关数据字典
	private List<SelectItem> itemsContractStatus = AppInitInfo.getDropdownList("ContractStatus","null"); 
	
	@Inject
	private DbAccess db;

	@PostConstruct
    public void init() {
    }
         
    //查询信息
	public String queryAction() {	
		StringBuilder sql = new StringBuilder("SELECT m FROM BmeContract m ");
		StringBuilder where = new StringBuilder(" WHERE ");
		String para = "";
		
		if(null!=io.getId() && !"".equals(io.getId())) {
			where.append("m.id=?1");
			para = io.getId();
		}
		else if(null!=io.getName() && !"".equals(io.getName())) {
			where.append("m.name like ?1");
			para = io.getName()+"%";
		}
		else if(null!=io.getPm() && !"".equals(io.getPm())) {
			where.append("m.pm=?1");
			para = io.getPm();
		}
		else if(null!=io.getConstruct() && !"".equals(io.getConstruct())) {
			where.append("m.construct=?1");
			para = io.getConstruct();
		}
		else if(null!=io.getStatus() && !"".equals(io.getStatus())) {
			where.append("m.status=?1");
			para = io.getStatus();
		}
		else {
			where = new StringBuilder("");
		}
		
		log.info("queryAction..." + sql);

		if(where.toString().equals(""))		
			voc = db.findBySql(sql.toString());
		else {
			sql.append(where);
			voc = db.findBySql(sql.toString(),new Object[]{para});
		}
		if(voc.size()>0) {
			selectedContract = voc.get(0);
			vod = selectedContract.getBmeContProds();
			vop = selectedContract.getBmeContPurches();			
		}
		return null;
	}

    public void onRowSelected(SelectEvent event) {
    	selectedContract = (BmeContract)event.getObject();
    	log.info("selectedContract..." + selectedContract);
    	vod = selectedContract.getBmeContProds(); 
    	vop = selectedContract.getBmeContPurches();	
    }
	
	//判断BigDecimal为null
	public static boolean isEmpty(BigDecimal b) {
		return b == null ;
	}

    public BmeContract getIo() {
		return io;
	}

	public void setIo(BmeContract io) {
		this.io = io;
	}

	public List<BmeContract> getVoc() {
		return voc;
	}

	public void setVoc(List<BmeContract> voc) {
		this.voc = voc;
	}

	public List<SelectItem> getItemsContractStatus() {
		return itemsContractStatus;
	}

	public void setItemsContractStatus(List<SelectItem> itemsContractStatus) {
		this.itemsContractStatus = itemsContractStatus;
	}

	public BmeContract getSelectedContract() {
		return selectedContract;
	}

	public void setSelectedContract(BmeContract selectedContract) {
		this.selectedContract = selectedContract;
	}

	public List<BmeContProd> getVod() {
		return vod;
	}

	public void setVod(List<BmeContProd> vod) {
		this.vod = vod;
	}

	public List<BmeContPurch> getVop() {
		return vop;
	}

	public void setVop(List<BmeContPurch> vop) {
		this.vop = vop;
	}
	
}